package swu.myt;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyFilter extends HttpFilter{
	private static final long serialVersionUID = 7208292752907758597L;

	public final static String LOGIN_STATUS = "LOGIN_STATUS";//状态值
	public final static String LOGIN_VALIDATE_CODE = "LOGIN_VALIDATE_CODE";//验证码值
	
	public void init(FilterConfig filterconfig) {
		System.out.println("initialized filter!");
	}

	@Override
	public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		HttpSession session = request.getSession(true);
		Boolean status = (Boolean) session.getAttribute(LOGIN_STATUS);//得到session的状态
		
		if(status==null||status.equals(Boolean.FALSE)){//不成功的状态		
			System.out.println(status);
			response.sendRedirect("../../myapp");//返回
			System.out.println("not success to enter!");
		}else {
			chain.doFilter(request, response);//成功状态
			
		}
		
	}
	
	public void destroy() {
		System.out.println("destroyed filter!");
	}

}
